/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.q1cc.cfs.jamp.players;

import java.io.*;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import net.q1cc.cfs.jamp.Main;
import net.q1cc.cfs.jamp.event.EventReporter;
import net.q1cc.cfs.jamp.event.EventType;
import net.q1cc.cfs.jamp.event.JAmpEvent;
import net.q1cc.cfs.jamp.event.JAmpSongEvent;
import net.q1cc.cfs.jamp.schema.Song;


/**
 *
 * @author claus
 */
@Deprecated
public class MPlayerHandler implements PlayerHandler {

    static HashMap<String,String> runtimeLocation = new HashMap<String,String>();
    OutputStream out;
    BufferedWriter outw;
    InputStream in;
    Process proc;
    PlayerListener listenT;
    BufferedReader read;
    static String loc;
    static final String args = " -cache 1048576 -slave ";
    boolean pause;
    Status status=Status.INIT;
    EventReporter eventR=Main.getInstance().getEventReporter();


    public MPlayerHandler()
    {
        initPlayer();
    }
    @Override
    public void initPlayer()
    {
            runtimeLocation.put("winxp", "resources\\mplayer\\mplayer.exe");
            loc = runtimeLocation.get("winxp");
            //proc = Runtime.getRuntime().exec(loc+args);

    }

    @Override
    public void destroy()
    {
        if(proc!=null)
            proc.destroy();
    }
    @Override
    public void play()
    {
        try {
            outw.append("play\n");outw.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void playpause()
    {
        try {
            outw.append("pause\n");outw.flush();
            pause=!pause;
            if(status==Status.PAUSED)
                status=Status.PLAYING;
            else if (status==Status.PLAYING)
                status=Status.PAUSED;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void seek(long timeInMs)
    {
        try {
            outw.append("seek "+timeInMs+" 2 \n");outw.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void playFile(String file)
    {
        try {
                URI uri = new URI(file);
                String path = uri.getPath();
                path = path.substring(1);
                path = path.replace("/", "\\");
                file = "\""+path+"\"";
                
            } catch (URISyntaxException ex) {
                ex.printStackTrace();
            }
        if (proc!=null)
        {
            try {
                proc.destroy();
                System.out.println(loc+args+file);

                proc = Runtime.getRuntime().exec(loc+args+file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        } else {
            try {
                System.out.println(loc+args+file);
                proc = Runtime.getRuntime().exec(loc+args+file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        in = proc.getInputStream();
        out = proc.getOutputStream();

        proc.getErrorStream();
        read = new BufferedReader(new InputStreamReader(in));
        outw=new BufferedWriter(new OutputStreamWriter(out));
        status=Status.STARTED;
        listenT=new PlayerListener();
        listenT.start();


    }
    @Override
    public void stop()
    {
        try {
            outw.append("stop\n");
            outw.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public Status getStatus()
    {
        return status;
    }

    public void playSong(Song song) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getPosition() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getDuration() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private class PlayerListener extends Thread
    {
        boolean run = true;
        Main main;
        MiniPlayerGui gui;
        MPlayerHandler playerHandler;
        private PlayerListener()
        {
            this.main=Main.getInstance();
            playerHandler=MPlayerHandler.this;
        }
        @Override
        public void run()
        {
            listen();
        }
        public boolean procIsRunning()
        {
            if (!run)
                return false;
            try {
                proc.exitValue();
            } catch (IllegalThreadStateException e)
            {
                return true;
            }
            return false;
        }
        
        private void listen()
        {
            while (procIsRunning())
            {
                try {
                    String line = read.readLine();
                    
                    if(line.startsWith("A: "))
                    {
                        status=Status.PLAYING;
                    } else if(line.equals("Exiting... (End of file)")) {
                        run=false;
                        gui.log(line);
                        status=Status.ENDED;
                        eventR.throwEvent(new JAmpSongEvent("Song ended", line, JAmpSongEvent.SONG_ENDED, null));
                    } else {
                        gui.log(line);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch(NullPointerException e)
                {
                    run=false;
                }
            }
        }

    }



    
    


}
